#include "Server.hpp"
#include <iostream>

namespace MRMDS::Server
{
using namespace boost::asio;
using ip::tcp;
bool Server::LoadConfig()
{
    return true;
}

void Server::InitService(DataBase::SharePtr db, int port)
{
    acceptor = std::make_unique<tcp::acceptor>(ioctx, tcp::endpoint(tcp::v4(), port));
    this->db = db;
    Accept();
}

void Server::Run()
{
    ioctx.run();
}

void Server::Accept()
{
    Session::SharePtr session = Session::CreatSharePtr(ioctx, db);
    acceptor->async_accept(session->Socket(), std::bind(&Server::OnAcceptClient, this, session, std::placeholders::_1));
}

void Server::OnAcceptClient(Session::SharePtr session, const boost::system::error_code &error)
{
    std::cout << "New Connected" << std::endl;
    if (!error)
    {
        boost::system::error_code code;
        session->Start();
    }
    Accept();
}
} // namespace MRMDS::Server
